#include "StdAfx.h"
#include "ConfigurationFile.h"


ConfigurationFile::ConfigurationFile()
{
	Init();
}


ConfigurationFile::~ConfigurationFile()
{
}

void ConfigurationFile::Init()
{
	CString path = AfxGetApp()->m_pszHelpFilePath;
	CString str = AfxGetApp()->m_pszExeName;
	CurFolder = path.Left(path.GetLength() - str.GetLength() - 4);
}

CString ConfigurationFile::GetProjectPath()
{
	return CurFolder;
}

void ConfigurationFile::OpenConfigFile(CString FileName)
{
	CurFilePath = CurFolder + FileName + ".proj";
}

void ConfigurationFile::WirteData(CString lpAppName, CString lpKeyName, CString lpString)
{
	if (CurFilePath.IsEmpty())
		return;
	WritePrivateProfileString(lpAppName, lpKeyName, lpString, CurFilePath);
}

void ConfigurationFile::WirteData(CString lpAppName, CString lpKeyName, INT lpValue)
{
	CString lpString;
	lpString.Format("%d", lpValue);
	WirteData(lpAppName, lpKeyName, lpString);
}

void ConfigurationFile::WirteData(CString lpAppName, CString lpKeyName, double lpValue)
{
	CString lpString;
	lpString.Format("%lf", lpValue);
	WirteData(lpAppName, lpKeyName, lpString);
}

CString ConfigurationFile::ReadData(CString lpAppName, CString lpKeyName)
{
	if (CurFilePath.IsEmpty())
		return "";
	char BUFF[2560];
	GetPrivateProfileString(lpAppName, lpKeyName, "", BUFF, sizeof(BUFF), CurFilePath);
	return BUFF;
}

BOOL ConfigurationFile::DeleteSection(CString lpAppName)
{
	if (CurFilePath.IsEmpty())
		return FALSE;
	WritePrivateProfileSection(lpAppName, NULL, CurFilePath);
	return TRUE;
}

BOOL ConfigurationFile::DeleteKey(CString lpAppName, CString lpKeyName)
{
	if (CurFilePath.IsEmpty())
		return FALSE;
	WritePrivateProfileString(lpAppName, lpKeyName, NULL, CurFilePath);
	return TRUE;
}

BOOL ConfigurationFile::GetProjectList(deque<CString> & ProjList)
{
	CFileFind finder;
	ProjList.clear();
	if (finder.FindFile(CurFolder + "*.proj")) {
		while (finder.FindNextFile()) {
			ProjList.push_back(finder.GetFileName());
		}
		ProjList.push_back(finder.GetFileName());
	}
	return TRUE;
}